// profile.js
const defaultAvatarUrl = '/images/default-avatar.png'

Page({
  data: {
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: ''
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile')
  },

  onLoad() {
    this.checkUserInfo()
  },

  checkUserInfo() {
    const userInfo = wx.getStorageSync('userInfo')
    if (userInfo) {
      this.setData({
        userInfo: userInfo,
        hasUserInfo: true
      })
    }
  },

  getUserProfile() {
    wx.getUserProfile({
      desc: '用于完善会员资料',
      success: (res) => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
        wx.setStorageSync('userInfo', res.userInfo)
      }
    })
  },

  // 点击订单类型
  onOrderTypeTap(e) {
    const type = e.currentTarget.dataset.type
    wx.navigateTo({
      url: `/pages/orders/orders?type=${type}`
    })
  },

  // 查看全部订单
  viewAllOrders() {
    wx.navigateTo({
      url: '/pages/orders/orders'
    })
  },

  // 菜单项点击
  onMenuItemTap(e) {
    const type = e.currentTarget.dataset.type
    switch(type) {
      case 'address':
        wx.navigateTo({ url: '/pages/address/address' })
        break
      case 'customer-service':
        // 调用客服
        wx.makePhoneCall({
          phoneNumber: '400-123-4567'
        })
        break
      case 'about':
        wx.navigateTo({ url: '/pages/about/about' })
        break
    }
  }
}) 